<?php
// +----------------------------------------------------------------------
// | Copyright (c) 2020 All rights reserved.
// +----------------------------------------------------------------------
// | User: Simony <1362229026@qq.com>
// | wechat: Shen86579595
// +----------------------------------------------------------------------
namespace app\common\command;

use AlibabaCloud\Chatbot\V20171011\Chat;
use think\console\Command;
use think\console\Input;
use think\console\input\Argument;
use think\console\input\Option;
use think\console\Output;


class Task extends Command
{
    protected $crontab_obj = null;
    protected $task_list = [];

    protected function configure()
    {

        $this->task_list = $this->getTask();
        $this->setName('crontab')
            ->addArgument('name', Argument::OPTIONAL, "your name")
            ->setDescription('Here is the crontab');
    }

    /**
     * User: Simony
     * Function: 执行任务
     * @param Input $input
     * @param Output $output
     * @return int|null|void
     */
    protected function execute(Input $input, Output $output)
    {

        foreach ($this->task_list as $obj) {
            $obj_name = "\app\common\command\crontab\\" . $obj['class_name'];
            if (class_exists($obj_name)) {
                $obj_name::execute($obj['function_name']);
            } else {
                echo $obj_name . '类不存在';
            }
        }

    }


    /**
     * User: Simony
     * Function: 获取任务
     */
    public function getTask()
    {
        $current_time = strtotime(date('Y-m-d H:i', time()));

        $where = [
            ['state', 'eq', 1],
            ['begin_time', 'elt', $current_time],
            ['end_time', 'egt', $current_time]
        ];
        $task_list = model('model/task/Task')->getList($where, 0);


        $data = [];

        foreach ($task_list as $task) {
            $is_effective_task = $this->isEffectiveTask($task, $current_time);
            if ($is_effective_task === true) $data[] = $task;
        }

        return $data;
    }


    /**
     * User: Simony
     * Function:是否有效任务
     * @param $task
     * @param $current_time
     * @return bool
     */
    public function isEffectiveTask($task, $current_time)
    {
        if ($task['begin_time'] > $current_time) return false; //未到时间的任务
        if ($task['end_time'] < $current_time) return false; //过期任务

        $type = $task['type']; //类型  0单次 1每天 2N天 3每小时 4N小时 5N分钟 6每星期 7每月
        if ($type == 0) return $task['cycle'] == $current_time ? true : false; //单次

        $begin_time =strtotime(date('Y-m-d H:i',$task['begin_time']));
        $cycle = $task['cycle']; //周期


        if ($type == 6) { //处理星期
            $w = date("w"); //星期几
            if ($w != $cycle) return false; //星期对不上
            return date('H:i', $current_time) == date('H:i', $begin_time) ? true : false;
        } elseif ($type == 7) { //月份
            return date('d H:i', $current_time) == date('d H:i', $begin_time) ? true : false;
        }

        $cycle_time = 0;
        switch ($type){
            case 1: //每天
                $cycle_time = 86400;
                break;
            case 2: // N天
                $cycle_time = $cycle * 86400;
                break;
            case 3: // 每小时
                $cycle_time = 3600;
                break;
            case 4: // N小时
                $cycle_time = $cycle * 86400;
                break;
            case 5:  //N分钟
                $cycle_time = $cycle * 60;
                break;
            default:
                break;
        }
        if ($cycle_time == 0) return false;
        return ($current_time - $begin_time) % $cycle_time == 0 ? true : false;
    }
}

